import { UI_Listen } from '../../config/ui_def'
import {  Sdk_Platform, OPPO_AD_CONFIG } from '../../frame/constants';

cc.Class({
    extends: cc.Component,

    properties: {
        img_sp: cc.Sprite,
        chayeimg_sp: cc.Sprite,
        icon: cc.Sprite,
        chayeicon: cc.Sprite,
        title: cc.Label,
        chayetitle: cc.Label,
        dec_lab: cc.Label,
        chayedec_lab: cc.Label,
        chayeNode: cc.Node,
        chaye: cc.Node,
        bannerNode: cc.Node,
        bannerPic: cc.Sprite,
        clickBtnTxt:cc.Label
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
        this.chaye.active = false
        this.chayeNode.active = false
        this.bannerNode.active = false
    },

    updatePage(param) {
        this.chaye.active = false
        this.chayeNode.active = false
        if(this.bannerNode.active){
            he.ad.changeBanner(false, this.bannerNode)
            this.bannerNode.active = false
        }
        this.adIndex = param.adIndex
        let isShowAd = param.isShowAd
        let isShowBanner = param.isShowBanner
        let posion = param.position

        this.adSwitch(isShowAd, isShowBanner,this.adIndex,posion)
    },

    closeUI() {
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_ENDAD })
        gg.eventManager.emit('playground', { type: UI_Listen.PLGD_ENDAD })
        this.adSwitch(false)
    },

    closeServer() {
        this.chayeNode.active = false
        this.bannerNode.active = false
        this.chaye.active=false
        this.uiBase.closePage()
    },

    adSwitch(status, isShowBanner,adIndex,posion) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    let ads = he.ad.getNativeJson(OPPO_AD_CONFIG.nativeAd[adIndex])
                    console.log(ads)
                    if (ads.icon) {
                        if (!isShowBanner) {
                            this.chayeNode.active = true
                            this.chaye.active=false
                            
                            this.chayeNode.setPosition(posion)
                            this.bannerNode.active = false
                            this.oppoAdInit(ads)
                        } else {
                            // //console.log("adsBanner")
                            this.chayeNode.active = false
                            this.bannerNode.active = true
                            this.chaye.active=false

                            this.oppoBannerInit(posion,adIndex)
                        }
                    } else {
                        if (!isShowBanner) {
                            // he.ad.loadInterstitial(true, this.bannerNode)
                        } else {
                            console.log("Banner")
                            this.chayeNode.active = false
                            this.bannerNode.active = true
                            this.chaye.active=false

                            this.oppoBannerInit(posion,adIndex)
                        }
                    }
                break
            }
        } else {
            if(this.bannerNode.active){
                // console.log("关掉关掉，不关掉没有美好未来")
                he.ad.changeBanner(false, this.bannerNode)
                this.bannerNode.active = false
            }
        }
    },

    oppoBannerInit(posion,adIndex) {
        this.bannerNode.setPosition(posion)
        this.bannerNode.name = OPPO_AD_CONFIG.bannerId[adIndex]
        he.ad.changeBanner(true, this.bannerNode)
    },

    ClickServer() {
        he.ad.clickNative(OPPO_AD_CONFIG.nativeAd[this.adIndex])
    },

    oppoAdInit(info) {
        let adInfo = info
        if (adInfo) {
            this.img_sp.active = false
            if (adInfo.icon) {
                this.loadImg(this.icon, adInfo.icon, 170, 170)
                this.icon.node.active = true
            } else {
                this.icon.node.active = false
            }

            this.title.string = `${adInfo.title}`
            this.dec_lab.string = `${adInfo.desc}`
            this.clickBtnTxt.string =`${adInfo.clickBtnTxt}`
        } 
    },
    oppoChayeAdInit(info) {
        let adInfo = info
        if (adInfo) {
            this.loadImg(this.chayeimg_sp, adInfo.iconUrlList[0], 600, 600)
            if (adInfo.icon) {
                this.loadImg(this.chayeicon, adInfo.icon, 170, 170)
                this.chayeicon.node.active = true
            } else {
                this.chayeicon.node.active = false
            }

            this.chayetitle.string = `${adInfo.title}`
            this.chayedec_lab.string = `${adInfo.desc}`
        } 
    },
    loadImg(container, url, width, height) {
        if (url.length < 4) {
            return he.error(("图片长度不对错误"));
        }
        let ext = url.substring(url.length - 4, url.length)
        ext = ext.toLowerCase()
        if (ext == 'jpeg') ext = '.jpg'
        cc.assetManager.loadRemote(url, { ext: ext }, (err, texture) => {
            if (err) {
                he.error(err)
            }
            if (this.callFun) {
                this.callFun()
            }
            var sprite = new cc.SpriteFrame(texture);
            container.spriteFrame = sprite;
            container.node.setContentSize(width, height)
        });
    },
});
